$(document).ready(function(){
	$("#school").change(function(){
		getClass();
	});
	$("#classes").change(function(){
		getStudent();
	});
    $.ajax({
        url:"../queryAllSchool",
        type:"post",
        success:function (data) {
            if(data.code===100){
                var schools=data.extend;
                console.log(schools);
                makeSchoolSelection(data);
            }

        }
    });
    function makeSchoolSelection(data) {
        $("#school").empty();
        var option=$("<option></option>").append("请选择学校").val(0);
        option.appendTo("#school")
        $.each(data.extend.schools,function(index,item){
            var option=$("<option></option>").append(item.schoolname).val(item.schoolid);
            option.appendTo("#school");
        });
    }
    function getClass() {
        var option=$("#school option:selected");
        var schoolId=option.val();
        if(option.val()!==0){
            $.ajax({
                url:"../queryClassBySchoolId",
                type:"get",
                data:{'schoolId':schoolId},
                success:function (data) {
                    if(data.code===100){
                        console.log(data);
                        makeClassSelection(data);
                        
                    }
                }

            });
        }
    }
    function makeClassSelection(data) {
        $("#classes").empty();
        var option=$("<option></option>").append("请选择班级").val(0);
        option.appendTo("#classes")
        $.each(data.extend.classInfo,function(index,item){
            var option=$("<option></option>").append(item.classname).val(item.classid);
            option.appendTo("#classes");
        });
    }
    function getStudent() {
        var option=$("#classes option:selected");
        	classId=option.val();
        if(option.val()!==0){
            $.ajax({
                url:"../queryRecord",
                type:"get",
                data:{'classInfo':classId},
                success:function (data) {
                    if(data.code===100){
                        console.log(data);
                       makeStudent(data);
                    }
                }

            });
        }
    }
    function makeStudent(data){
    	$("#table1 tbody").empty();
    	$.each(data.extend.record,function(index,item){
    		var trParent=$("<tr></tr>").css("text-align","center");
    		$("<td></td>").append(item.student.studentnum).appendTo(trParent);
    		$("<td></td>").append(item.student.studentname).appendTo(trParent);
    		$("<td></td>").append(item.student.sex=1?"男":"女").appendTo(trParent);
    		$("<td></td>").append(ChangeDateFormat(item.createtime)).appendTo(trParent);
    		$("<td></td>").append(item.rule.rulename).appendTo(trParent);
    		$("<td></td>").append(item.rule.deal).appendTo(trParent);
    		var btn_edit=$("<button></button>").append($("<input>").attr("type","hidden").addClass("id").attr("value",item.student.studentnum));
    		var btn_delete=$("<button></button>").append($("<input>").attr("type","hidden").addClass("id").attr("value",item.student.studentnum));
    		btn_edit.append($("<span></span>").addClass("glyphicon glyphicon-edit")).addClass("btn btn-warning btn-sm");
			btn_delete.append($("<span></span>").addClass("glyphicon glyphicon-trash")).addClass("btn btn-danger btn-sm");
			btn_edit.css("margin-right","10px");
    		$("<td></td>").append(btn_edit).append(btn_delete).appendTo(trParent);
    		
    		trParent.appendTo("#table1 tbody");
    	})
    }
    function makePage(result){
    	$("#page_Info").empty();
    	var center=$("<center></center>").append("当前第"+result.extend.pageInfo.pageNum+"页,总共"+result.extend.pageInfo.pages+
    			" 页,总"+result.extend.pageInfo.total+" 条数记录");
    	$("#page_Info").append(center);
    	
    }
    function build_page_nav(result){
    	$("#page_nav").empty();
    	var ul=$("<ul></ul>").addClass("pagination");
    	var firstPage=$("<li></li>").append($("<a></a>").append("首页").attr("href","#"));
    	var prePage=$("<li></li>").append($("<a></a>").append("&laquo;").attr("href","#"));
    	if(result.extend.pageInfo.hasPreviousPage==false){
    		firstPage.addClass("disabled");
    		prePage.addClass("disabled");
    	}else{
    		//为元素添加点击事件
    		firstPage.click(function(){
    			to_Page(1);
    		});
    		prePage.click(function(){
    			to_Page(result.extend.pageInfo.pageNum-1);
    		});
    	}
    	
    	
    	var nextPage=$("<li></li>").append($("<a></a>").append("&raquo;").attr("href","#"));
    	var lastPage=$("<li></li>").append($("<a></a>").append("末页").attr("href","#"));
    	if(result.extend.pageInfo.hasNextPage==false){
    		nextPage.addClass("disabled");
    		lastPage.addClass("disabled");
    	}else{
    		lastPage.click(function(){
    			to_Page(result.extend.pageInfo.pages);
    		});
    		nextPage.click(function(){
    			to_Page(result.extend.pageInfo.pageNum+1);
    		});
    	}
    	
    	
    	ul.append(firstPage).append(prePage);
    	$.each(result.extend.pageInfo.navigatepageNums,function(index,item){
    		var num=$("<li></li>").append($("<a></a>").append(item).attr("href","#"));
    		if(result.extend.pageInfo.pageNum==item){
    			num.addClass("active");
    		}
    		num.click(function(){
    			to_Page(item);
    		});
    		ul.append(num);
    	});
    	ul.append(nextPage).append(lastPage);
    	var nav=$("<nav></nav>").addClass("Page navigation").append(ul);
    	nav.appendTo("#page_nav");
    }
    function to_Page(pn){
    	$.ajax({
    		url:"../queryAllStudentWithClass",
    		data:{"classId":classId,"pn":pn},
    		type:"get",
    		success:function(result){
    			console.log(result);
    			makeMenu(result);
    			makePage(result);
    			build_page_nav(result);
    		}
    	})
    }
    function ChangeDateFormat(cellval) {
	    var date = new Date(parseInt(cellval, 10));
	    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
	    var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
	    return date.getFullYear() + "-" + month + "-" + currentDate;
	}
});